package com.example.wei.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import com.example.wei.mvp.R;
import com.example.wei.mvp.base.BaseRVActivity;
import com.example.wei.mvp.component.AppComponent;
import com.example.wei.mvp.component.DaggerMainComponent;
import com.example.wei.mvp.contract.HelpContract;
import com.example.wei.mvp.ui.bean.PersonResult;
import com.example.wei.mvp.ui.bean.TestBean;
import com.example.wei.mvp.ui.presenter.HelpPresenter;
import com.example.wei.mvp.util.ChannelUtils;
import com.example.wei.mvp.util.L;
import com.example.wei.mvp.util.SPUtils;
import com.example.wei.mvp.util.ToastUtil;
import com.githang.statusbar.StatusBarCompat;
import com.umeng.analytics.MobclickAgent;

import butterknife.OnClick;

public class MainActivity extends BaseRVActivity<HelpPresenter> implements HelpContract.View {

    private long oldTime;

    @Override
    public int getLayoutId() {
        SPUtils.put("isFirst", false);
        return R.layout.activity_main;
    }

    @Override
    protected void innitView() {
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#00000000"), true);
    }

    @Override
    protected void innitData() {
       ToastUtil.showToast(ChannelUtils.getChannel());
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getUserStatus();

    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMainComponent.builder().appComponent(appComponent).build().inject(this);
    }


    @Override
    public void getUserStatus(TestBean data) {

        L.d("TestBean", ">>>>>>>>>>>>>>>>" + data.toString());
        if (0 == data.getCode()) {
            ToastUtil.showToast(data.getResult().getBankName());
        }
    }

    @Override
    public void getUserInfo(PersonResult person) {
        if (0 == person.getCode()) {
            ToastUtil.showToast(person.getResult().getName());
        }
    }

    @Override
    public void getUserPost(PersonResult person) {
        if (0 == person.getCode()) {
            ToastUtil.showToast(person.getMessage());
        }
    }

    @Override
    public void showError(String err) {

    }

    @Override
    public void complete() {

    }


    @OnClick({R.id.activity_two, R.id.activity_login,R.id.activity_test,R.id.btn_post})
    public void onViewClicked(View view) {
        String sessionid = (String) SPUtils.get("sessionid", "");
        switch (view.getId()) {
            case R.id.activity_two:
                mPresenter.getUserStatus();
                if (TextUtils.isEmpty(sessionid)) {
                }else {
                    startActivity(new Intent(this, SettingActivity.class));
                }

                break;
            case R.id.activity_login:
                if (TextUtils.isEmpty(sessionid)) {
                    startActivity(new Intent(this, LoginActivity.class));
                }else {
                    ToastUtil.showToast("您已登录");
                }
                break;
            case R.id.activity_test:
                mPresenter.getUserInfo("刘西美子",26,"女");
                break;
            case R.id.btn_post:
                mPresenter.getUserPost("李萌萌",22,"女");
                break;
        }
    }

    /**
     * 再点一次退出程序
     * @param keyCode
     * @param event
     * @return
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if ((System.currentTimeMillis() - oldTime) > 2000) {
           ToastUtil.showToast("再按一次退出程序");
            oldTime = System.currentTimeMillis();
        } else {
            MobclickAgent.onKillProcess(MainActivity.this);
            finish();
            System.exit(0);
        }
    }


}
